# -*- encoding=utf8 -*-

import os
import sys
import shutil

# 获取待添加的PodList
def getPodList():
    resultList = []
    resultList.append("UTDID")

    return resultList

# 获取目录下的所有文件夹名称
def getDirList(path):
    resultList = []

    fileList = os.listdir(path)
    for i in range(len(fileList)):
        fileName = fileList[i]
        if os.path.isdir(os.path.join(path, fileName)):
            resultList.append(fileName)

    return resultList


# 枚举cocoapods仓库
def enumeratePodSpecs(path, callback, tag=None):
    topFolderList = getDirList(path)

    for top in topFolderList:
        firstFolder = path + "%s/" % top
        middleFolderList = getDirList(firstFolder)

        for middle in middleFolderList:
            secondFolder = firstFolder + "%s/" % middle
            bottomFolderList = getDirList(secondFolder)

            for bottom in bottomFolderList:
                thirdFolder = secondFolder + "%s/" % bottom

                destFolderList = getDirList(thirdFolder)

                for file_name in destFolderList:
                    callback(file_name, thirdFolder + "%s/" % file_name, tag)


def _copyFile(srcPath, destPath, removeOld=False):
    if os.path.isdir(destPath) == True and os.path.exists(destPath) == False:
        os.makedirs(destPath)

    if os.path.isfile(srcPath):
        target_path = ""
        if os.path.isdir(destPath) or destPath.endswith("/"):
            if destPath.endswith("/"):
                target_path = destPath + _getLastComponent(srcPath)
            else:
                target_path = destPath + "/" + _getLastComponent(srcPath)
        else:
            target_path = destPath

        if os.path.exists(target_path) == True and removeOld:
            os.remove(target_path)

        print(target_path)
        _createFolderIfNeeded(target_path)
        print(target_path)

        shutil.copyfile(srcPath, target_path)

    elif os.path.isdir(srcPath) == True:
        if os.path.exists(destPath) == True and removeOld:
            os.removedirs(destPath)

        shutil.copytree(srcPath, destPath)


# 获取路径中的最后一个文件(夹)名称
def _getLastComponent(path):
    tmpList = path.split("/")
    for item in reversed(tmpList):
        if len(item) > 0:
            return item
    return ""


def _createFolderIfNeeded(path):
    if os.path.isdir(path):
        if os.path.exists(path) == False:
            os.makedirs(path)
    else:
        index = path.rfind("/")
        if index >= 0:
            folder = path[0:index+1]
            if os.path.exists(folder) == False:
                os.makedirs(folder)


def _callback(podName, path, tag):
    if tag == "import":
        if podName in podList:
            _copyFile(path, testFolder + podName + "/")
            print("copy: " + podName + "    " + path)


if __name__ == "__main__":
    specsFolder = sys.path[0] + "/../Specs/"
    testFolder = sys.path[0] + "/../test/"

    # podList = getDirList(folder)
    podList = getPodList()

    targetFolder = "../../00/cocoapods/Specs/"
    enumeratePodSpecs(path=targetFolder, callback=_callback, tag="import")
